import Vue from 'vue'
import Vuex from 'vuex'
import global from '../common/global.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		socketTask: null,
		websocketData: {}, // 存放从后端接收到的websocket数据
		user: "",
		heartbeatInterval: null,
		timeOut: 10000, //10秒心跳发送
		form: {
			clientId:null,
			doctorId:null,
			morbidityCondition: 0, //发病情况 0初诊 1复诊
			timesNum: null, //复诊次数 初诊不填 复诊必填
			morbidityTime: null, //发病时间 初诊必填 复诊非必填
			feverDayNum: null, //发热天数
			highTemperature: null, //发热最高温度
			feverSymptom: 0, //发热症状 0无 1有
			symptomDetailArr: [], //发热症状详情
			feverTimeInterval: 0, //发热时间间隔 0持续发热 1间隔
			intervalTimeHour: null, //间隔小时
			feverMedicateStatus: 0, //发热用药 0无 1有
			medicateDetail: null, //用药详情
			heelExampleUrlArr: [], //足跟示例资源地址
			heelExampleType: null, //足跟示例资源类型 0图片 1视频
			heelColor: null, //足跟颜色
			heelTemperature: null, //足跟温度 1温 2凉 3热
			heelPressRedTime: null, //足跟按压变红 0 <=3秒  1>3秒
			heelHardness: null, //足心软硬度 1软(唇) 2韧(鼻) 3硬(额)
			coughDayNum: null, //咳嗽天数
			aggravateDayNum: null, //加重天数
			coughTimesArr: [], //咳嗽时间
			coughRelatedFactor: 0, //咳嗽相关因素0无 1有
			coughFactorDetailArr: [], //咳嗽相关因素详情
			coughVomitStatus: 0, //咳嗽呕吐 0无 1有
			coughAwakeningStatus: 0, //夜里咳醒 0无 1有
			feverCoughStatus: 0, //发热咳嗽 0无 1有
			coughPeculiarityArr: [], //咳嗽特点
			coughRecordedUrl: null, //咳嗽录音 可上床多条 用,隔开
			coughRecordedUrlArr: [], //咳嗽录音数组
			gaspStatus: 0, //喘息 0无 1有
			gaspDayNum: null, //喘息天数
			sneezeStatus: 0, //喷嚏 0无 1有
			sneezeDayNum: null, //喷嚏天数
			nasalObstructionStatus: 0, //鼻塞 0无 1有
			nasalObstructionDayNum: null, //鼻塞天数
			anginaStatus: 0, //咽痛 0无 1有
			anginaDayNum: null, //咽痛天数
			dullStatus: 0, //暗哑 0无 1有
			dullDegree: 1, //暗哑程度 1轻 2中 3重
			gaspBreathVideoUrlArr: [], //喘息或呼吸困难视频示例地址
			electronicStethoscopeUrlArr: [], //电子听诊器呼吸音地址
			kneadNoseEye: 0, //手揉鼻子眼睛 0无 1有
			eczemaStatus: 0, //幼时湿疹 0无 1有
			eczemaDegree: 1, //湿疹程度 1轻 2中 3重
			allergyRhinitis: 0, //过敏性鼻炎 0无 1有
			parentAllergyRhinitis: 0, //父母过敏性鼻炎 0无 1有
			allergySource: 0, //过敏原 0无 1有
			allergySourceDetail: null, //过敏原详情
			penicillinCephalosporinAllergy: 0, //青霉素/头孢过敏史 0无 1有
			otherAllergyMedicineDetail: null, //其他过敏药物
			foodAllergyMedicineDetail: null, //食物过敏详情
			recentRash: 0, //近两天皮疹 0无 1有
			lowerPartRash: 0, //下面部位皮疹 0无 1有
			lowerPartRashDetailArr: [], //下面部位皮疹详情
			rashShowType: null, //皮疹展示 0图片 1视频
			rashShowUrlArr: [], //皮疹展示资源地址
			otherExpression: 0, //有无下列表现 0无 1有
			otherExpressionDetailArr: [], //有下列表现详情
			otherIllness: 1, //其他疾病
			otherIllnessDetail: null, //其他疾病详情
			temperatureCheckArr: [],
			electronicTemperatureUrlArr:[],
			physicalAssayImageArr: [], //血常规详情
			physicalUrineReportArr: [], //尿常规详情
			chestRadiograph: 0, //是否有胸片
			chestRadiographArr: [], //胸片详情
			lungCt: 0, //是否有肺部CT
			lungCtArr: [], //肺部CT详情
			carbonMonoxideArr: [], //一氧化碳检测详情
			lungFeature: 0, //是否有肺功能
			lungFeatureArr: [], //肺功能检测详情
			bronchus: 0, //是否有支气管检查
			bronchusArr: [], //支气管检查详情
			illDayNum: null,
			takeOrallyArr: [],
			intravenousDripArr: [],
			atomizationArr: [],
			otherMedicalHistoryArr: [] //图片和pdf都支持
		},
		clientInfo:{
			realName:'请选择就诊人'
		}
	},
	mutations: {
		setWebsocketData(state, data) {
			let message = JSON.parse(data.data);
			state.websocketData = message
		},
		setUser(state, user) {
			state.user = user
			if(user){
				this.commit('getInterrogationInfo')
			}
		},
		setInterrogationClientInfo(state,info){
			state.form.clientId = info.id
			state.clientInfo = info
		},
		setInterrogationInfo(state,info){
			state.form = info
		},
		getInterrogationInfo(state){
			uni.request({
				url:'http://192.168.0.129:10086/patient-interrogationRecords/findNotSubmit',
				method:'POST',
				header:{
					token:uni.getStorageSync('token')
				},
				success: (res) => {
					if(res.data.code===200){
						if(res.data.data){
							state.form = res.data.data
						}
					}
				}
			})
		}
	},
	actions: {
		websocketInit({
			state,
			dispatch
		}, userId) {
			let url = global.ws + "?userId=" + userId + "&token=" + uni.getStorageSync('token')
			state.socketTast = uni.connectSocket({
				url, // url是websocket连接ip
				success: () => {
					console.log('websocket连接中...')
				},
				fail: e => {
					console.log('连接失败' + e)
				}
			})
			state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
			state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
			state.socketTast.onClose(e => dispatch('websocketOnClose'))
			state.socketTast.onError(e => dispatch('websocketOnError'))
		},
		websocketOnOpen({
			state,
			commit,
			dispatch
		}) {
			dispatch('startHeart')
			console.log('WebSocket连接成功！')
		},
		// 收到数据
		websocketOnMessage({
			commit
		}, res) {
			if (res.data == '失败') {
				uni.showToast({
					icon: 'none',
					title: '请勿发送敏感词汇'
				})
				return false
			}
			commit('setWebsocketData', res)
		},
		websocketOnClose({
			commit,
			state,
			dispatch
		}) {
			state.socketTast = null
			console.log('WebSocket连接关闭')
			if (state.user) {
				dispatch('websocketInit', state.user)
			}
			if (state.heartbeatInterval) {
				clearInterval(state.heartbeatInterval)
			}
		},
		websocketOnError({
			commit,
			dispatch
		}) {
			console.log('WebSocket连接错误')
		},
		websocketClose({
			state,
			dispatch
		}) {
			if (!state.socketTast) {
				return false
			}
			state.socketTast.close({
				success(res) {
					console.log('关闭成功', res)
					if (state.heartbeatInterval) {
						clearInterval(state.heartbeatInterval)
					}
				},
				fail(err) {
					console.log('关闭失败', err)
				}
			})
		},
		//检测心跳
		startHeart({
			state,
			dispatch
		}) {
			if (state.heartbeatInterval) {
				clearInterval(state.heartbeatInterval)
			}
			state.heartbeatInterval = setInterval(() => {
				dispatch('websocketSend', 'ping')
			}, state.timeOut)
		},
		// 发送数据
		websocketSend({
			state,
			dispatch
		}, data) {
			if (!state.socketTast) {
				console.log('发送失败,未连接')
				return false
			}
			uni.sendSocketMessage({
				data,
				success: res => {
					// console.log('发送成功', res)
				},
				fail: e => {
					console.log('发送失败', e)
				}
			})
		},


	}

})

export default store